package com.fs.service;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.fs.auth.TokenGenerator;
import com.fs.mapper.SysAdminMapper;
import com.fs.mapper.SysAdminMenusMapper;
import com.fs.mapper.SysAdminRolesMapper;
import com.fs.mapper.SysAdminTokenMapper;
import com.fs.pojo.SysAdmin;
import com.fs.pojo.SysAdminRoles;
import com.fs.pojo.SysAdminToken;
import com.fs.service.ShiroService;
import com.fs.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service("shiroservice")
public class ShiroServiceImpl implements ShiroService {
    //12小时后失效
    private final static int EXPIRE = 12;

    @Autowired
    private SysAdminMapper userRepository;


    @Autowired
    private SysAdminTokenMapper sysTokenRepository;

    @Autowired
    private SysAdminRolesMapper sysAdminRolesDao;

    @Autowired
    private SysAdminMenusMapper sysAdminMenusMapper;


    @Override
    public SysAdmin findByUsername(String username) {
        return userRepository.findByUsername(username);
    }

    @Transactional
    @Override
    public SysResult createToken(Integer userId) {
        SysResult result = new SysResult();
        String token = TokenGenerator.generateValue();
//        LocalDateTime now = LocalDateTime.now();
        Date now = new Date();
//        LocalDateTime expireTime = now.plusHours(EXPIRE);
        DateTime expireTime = DateUtil.offsetHour(now, EXPIRE);
        SysAdminToken tokenEntity= sysTokenRepository.findByUserId(userId);
        if(tokenEntity==null){
            tokenEntity=new SysAdminToken();
            tokenEntity.setUserId(userId);
            tokenEntity.setToken(token);
            tokenEntity.setUpdated(now);
            tokenEntity.setEndTime(expireTime);
        }else{
            tokenEntity.setToken(token);
            tokenEntity.setUpdated(now);
            tokenEntity.setEndTime(expireTime);
        }
        sysTokenRepository.insert(tokenEntity);
        Map<String,Object> map=new HashMap<>();
        map.put("token",token);
        map.put("expire",expireTime);
        result.setData(map);
        return result;
    }

    @Override
    public SysAdminToken findByToken(String accessToken) {
        return sysTokenRepository.findByToken(accessToken);

    }

    @Override
    public SysAdmin findByUserId(Integer userId) {
        SysAdmin admin = userRepository.findByUserId(userId);
        List<SysAdminRoles> list = new ArrayList<>();
        //超级管理员
        if(userId==1){
            SysAdminRoles rolesEntity = new SysAdminRoles();
            rolesEntity.setRoleName("超级管理员")
                    .setPermissions(sysAdminMenusMapper.selectList(null));

            list.add(rolesEntity);
            return admin.setRoles(list);
        }
        // 其他角色
        List<SysAdminRoles> roles=sysAdminRolesDao.selectByUserId(userId);
        admin.setRoles(roles);

        for (SysAdminRoles role : roles) {
            role.setPermissions(sysAdminMenusMapper.selectMenus(role.getId()));
        }
        return admin;
    }

    @Transactional
    @Override
    public void logout(String token) {
        SysAdminToken byToken = findByToken(token);
        //生成一个token
        token = TokenGenerator.generateValue();
        //修改token
        byToken.setToken(token).setUpdated(new Date());
        //使前端获取到的token失效
        sysTokenRepository.updateById(byToken);
    }
}
